package com.nsgf.library.ui.page;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DiffUtil;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderBean;
import com.nsgf.library.data.repository.DataRepository;
import com.nsgf.library.data.response.DataResult;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.databinding.ActivityFloodSituationBriefingBinding;
import com.nsgf.library.databinding.ItemFloodSituationBriefingBinding;
import com.nsgf.library.ui.adapter.SimpleDataBindingPositionAdapter;
import com.nsgf.library.ui.adapter.ViewHolder;
import com.nsgf.library.utils.ToastUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * 汛情简报
 */
public class FloodSituationBriefingActivity extends BaseActivity {

    private StateHolder mStates;

    private SimpleDataBindingPositionAdapter<WorkOrderBean, ItemFloodSituationBriefingBinding> mAdapter;

    @Override
    protected void initViewModel() {

        mStates = new StateHolder();

    }

    ActivityFloodSituationBriefingBinding mBinding;

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingPositionAdapter<WorkOrderBean, ItemFloodSituationBriefingBinding>(this, R.layout.item_flood_situation_briefing, new DiffUtil.ItemCallback<WorkOrderBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull WorkOrderBean oldItem, @NonNull WorkOrderBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull WorkOrderBean oldItem, @NonNull WorkOrderBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemFloodSituationBriefingBinding binding, WorkOrderBean item, ViewHolder holder, int position) {

            }
        };
        return new DataBindingConfig(R.layout.activity_flood_situation_briefing, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = (ActivityFloodSituationBriefingBinding) getBinding();
        mStates.titleBar.set("汛情简报");

        mBinding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getDatas();
            }
        });
        mBinding.smartRefreshLayout.autoRefresh();
        mBinding.recyclerView.setItemAnimator(null);
    }


    public void getDatas() {
        DataRepository.getInstance()
                .workOrderList(UserInfo.getInstance().getProjectCode(),
                        0,
                        "",
                        "",
                        "",
                        "",
                        "")
                .subscribe(new io.reactivex.Observer<DataResult<ResponseStatus<List<WorkOrderBean>>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(DataResult<ResponseStatus<List<WorkOrderBean>>> dataResult) {
                        if (dataResult.getResponseStatus().isSuccess()) {
                            if (dataResult.getResult().getResponseCode() == 200) {
                                mAdapter.submitList(dataResult.getResult().getData());

                                if (dataResult.getResult().getData().size() > 0) {
                                    mBinding.notdata.setVisibility(View.GONE);
                                } else {
                                    mBinding.notdata.setVisibility(View.VISIBLE);
                                }

                            }

                        } else {
                            ToastUtils.showLongToast(getErrorMsg(dataResult.getResponseStatus().getE()));
                        }
                        mBinding.smartRefreshLayout.finishRefreshWithNoMoreData();

                    }

                    @Override
                    public void onError(Throwable e) {

                        mBinding.smartRefreshLayout.finishRefresh();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
